from ai_assistant import AIAssistant
from voice_assistant import VoiceAssistant
from text_generation_assistant import TextGenerationAssistant
from image_generation_assistant import ImageGenerationAssistant
from data_analysis_assistant import DataAnalysisAssistant
from translation_assistant import TranslationAssistant

class AIManager:
    def __init__(self, max_size):
        self.assistant_list = []
        self.max_size = max_size
        self.init_default_assistants()
    
    def init_default_assistants(self):
        self.add_assistant(VoiceAssistant("101", "小智", "v1.2", True, 85, "中文,英文"))
        self.add_assistant(TextGenerationAssistant("102", "文小助", "v2.0", True, 92, "诗歌,文案,报告"))
        self.add_assistant(ImageGenerationAssistant("103", "图灵", "v1.5", False, 88, "4K,8K"))
        self.add_assistant(DataAnalysisAssistant("104", "数小析", "v1.0", True, 79, "CSV,Excel,JSON"))
        self.add_assistant(TranslationAssistant("105", "翻译精灵", "v2.1", False, 95, "中英,中日,中韩"))
    
    def add_assistant(self, assistant):
        if len(self.assistant_list) >= self.max_size:
            return "❌ 系统容量不足，无法添加新助手！"
        
        if self.find_assistant_by_id(assistant.get_id()) is not None:
            return f"❌ ID为{assistant.get_id()}的助手已存在，添加失败！"
        
        self.assistant_list.append(assistant)
        return f"✅ 助手【{assistant.get_name()}】添加成功！"
    
    def find_assistant_by_id(self, id):
        for assistant in self.assistant_list:
            if assistant.get_id() == id:
                return assistant
        return None
    
    def get_all_assistants(self):
        return self.assistant_list.copy()
    
    def update_assistant(self, id, property, value):
        assistant = self.find_assistant_by_id(id)
        if assistant is None:
            return f"❌ 未找到ID为{id}的助手，修改失败！"
        
        property = property.lower()
        if property == "name":
            assistant.set_name(value)
        elif property == "type":
            assistant.set_type(value)
        elif property == "version":
            assistant.set_version(value)
        elif property == "online":
            assistant.set_online(value == "1")
        elif property == "score":
            try:
                score = int(value)
                if score < 0 or score > 100:
                    return "❌ 能力评分需在0-100之间，修改失败！"
                assistant.set_ability_score(score)
            except ValueError:
                return "❌ 评分需为数字，修改失败！"
        else:
            return f"❌ 不支持修改属性：{property}，可修改属性为：name,type,version,online,score"
        
        return f"✅ 助手【{id}】修改成功！"
    
    def delete_assistant(self, id):
        assistant = self.find_assistant_by_id(id)
        if assistant is None:
            return f"❌ 未找到ID为{id}的助手，删除失败！"
        
        self.assistant_list.remove(assistant)
        return f"✅ 助手【{assistant.get_name()}】已删除。"
    
    def start_assistant(self, id):
        assistant = self.find_assistant_by_id(id)
        if assistant is None:
            return f"❌ 未找到ID为{id}的助手，启动失败！"
        if assistant.is_online_status():
            return f"ℹ️  助手【{assistant.get_name()}】已在运行。"
        
        assistant.set_online(True)
        return f"✅ 助手【{assistant.get_name()}】启动成功，当前状态：在线。"
    
    def stop_assistant(self, id):
        assistant = self.find_assistant_by_id(id)
        if assistant is None:
            return f"❌ 未找到ID为{id}的助手，停止失败！"
        if not assistant.is_online_status():
            return f"ℹ️  助手【{assistant.get_name()}】已停止运行。"
        
        assistant.set_online(False)
        return f"✅ 助手【{assistant.get_name()}】已停止，当前状态：离线。"
    
    def assign_task(self, id, task_content):
        assistant = self.find_assistant_by_id(id)
        if assistant is None:
            return f"❌ 未找到ID为{id}的助手，无法分配任务！"
        if not assistant.is_online_status():
            return f"❌ {assistant.get_name()} 当前离线，无法执行任务。"
        
        return assistant.execute_task(task_content)
    
    def get_statistics(self):
        total = len(self.assistant_list)
        online_count = 0
        total_score = 0
        
        for assistant in self.assistant_list:
            if assistant.is_online_status():
                online_count += 1
            total_score += assistant.get_ability_score()
        
        offline_count = total - online_count
        avg_score = 0 if total == 0 else total_score / total
        
        return (f"系统统计信息：\n" \
                f"总助手数: {total}\n" \
                f"在线助手数: {online_count}\n" \
                f"离线助手数: {offline_count}\n" \
                f"平均能力评分: {avg_score:.2f}")